<template>
  <div class="carousel-wrapper" @mouseenter="stop" @mouseleave="start">
    <!--    图片容器 -->
    <ul class="carousel-body">
      <li v-for="item in img" :key="item.id" :class="{fade: index === item.id}">
        <img :src="item.url" alt="">
      </li>
    </ul>
    <!--    上一页 -->
    <a href="javascript:;" class="carousel-btn left" @click="prev"><i class="iconfont icon-angle-left"></i></a>
    <!--    下一页 -->
    <a href="javascript:;" class="carousel-btn right" @click="next"><i class="iconfont icon-angle-right"></i></a>
    <!--    指示器 -->
    <div class="carousel-indicator">
      <span v-for="item in img" :key="item.id" :class="{fade: index === item.id}" @click="point(item.id)"></span>
    </div>
  </div>
</template>

<script>
export default {
  name: 'App',
  props: {
    // 必须传入图片数组
    img: {
      type: Array,
      required: true
    },
    // 是否自动播放，默认自动播放
    autoPlay: {
      type: Boolean,
      default: true
    },
    // 播放速度
    speed: {
      type: Number,
      default: 2000
    }
  },
  data () {
    return {
      index: 0
    }
  },
  mounted () {
    this.firstStart()
  },
  methods: {
    // 自动播放
    autoPlayFn () {
      if (this.index >= this.img.length - 1) {
        this.index = 0
        return
      }
      this.index++
    },
    // 首次开始加载
    firstStart () {
      // 先清除定时器，防止定时器没有清楚干净
      clearInterval(this.timer)
      // 开始定时器
      this.timer = setInterval(() => {
        if (this.autoPlay) {
          this.autoPlayFn() // 执行自动播放
        }
      }, this.speed)
    },
    // 鼠标移入，停止自动播放
    stop () {
      if (this.timer) {
        clearInterval(this.timer)
      }
    },
    // 鼠标移出开始自动播放
    start () {
      this.firstStart()
    },
    // 上一页
    prev () {
      if (this.index === 0) {
        this.index = this.img.length - 1
        return
      }
      this.index--
    },
    // 下一页
    next () {
      if (this.index === this.img.length - 1) {
        this.index = 0
        return
      }
      this.index++
    },
    // 点击指示器，跳转至对应图片
    point (id) {
      this.index = id
    }
  },
  // 离开组件后，销毁此组件的定时器
  unmounted () {
    clearInterval(this.timer)
  }
}
</script>

<style scoped lang="less">
.carousel-wrapper {
  width: 1024px;
  height: 768px;
  position: relative;

  .carousel-body {
    width: 100%;
    height: 100%;
    position: relative;
    top: 0;
    left: 0;

    li {
      position: absolute;
      top: 0;
      left: 0;
      opacity: 0;
      transition: opacity .5s linear;

      img {
        width: 100%;
        height: 100%;
      }

      &.fade {
        z-index: 1;
        opacity: 1;
      }
    }
  }

  .carousel-btn {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    position: absolute;
    top: 50%;
    z-index: 2;
    text-align: center;
    line-height: 44px;
    opacity: 0;
    transition: all 0.5s;

    i {
      font-size: 44px;
    }

    &.left {
      left: 20px;
    }

    &.right {
      right: 20px
    }
  }

  .carousel-indicator {
    width: 100%;
    position: absolute;
    bottom: 0;
    left: 50%;
    z-index: 2;

    span {
      display: inline-block;
      width: 12px;
      height: 12px;
      border-radius: 50%;
      background: rgba(0, 0, 0, 0.8);
      border: 1px solid black;
      cursor: pointer;
      margin-right: 5px;

      &.fade {
        background: white;
      }
    }
  }

  &:hover {
    .carousel-btn {
      opacity: 1;
    }
  }
}
</style>
